local heming_jink = fk.CreateSkill {
  name = "3s__heming_jink",
  tags = { "Turn" },
}

Fk:loadTranslationTable{
  ["3s__heming_jink"] = "合鸣[闪]",
  [":3s__heming_jink"] = "每回合限一次，你可以将一名角色的一张牌当【闪】使用，然后令其将手牌数摸至其体力上限。",
}

local U = require "packages/utility/utility"

heming_jink:addEffect("viewas", {
  pattern = "jink",
  prompt = "#3s__heming-jink",
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    local jink = Fk:cloneCard("jink")
    jink.skillName = "3s__heming"
    return jink
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function(self, player, response)
    return player:getMark("heming-jink-turn") == 0 and not response
  end,
  before_use = function(self, player, use)
    local room = player.room
    local tos = table.filter(room:getAlivePlayers(), function (p) return not p:isNude() end)
    if #tos == 0 then
      U.SetSwitchSkillState(player, "3s__heming", fk.SwitchYin)
      return ""
    end
    local to = room:askToChoosePlayers(player, {
      targets = tos,
      max_num = 1,
      min_num = 1,
      skill_name = "3s__heming",
      cancelable = false,
      prompt = "#3s__heming-choose",
    })
    if #to == 0 then
      U.SetSwitchSkillState(player, "3s__heming", fk.SwitchYin)
      return ""
    end
    local card = player.room:askToChooseCard(player, {
      skill_name = "3s__heming",
      target = to[1],
      flag = "he",
      prompt = "#3s__heming-jink-card::"..to[1].id,
    })
    if card then
      room:addPlayerMark(player, "heming-jink-turn")
      use.card:addSubcard(card)
      use.extra_data = use.extra_data or {}
      use.extra_data.target = to[1]
    else
      return ""
    end
  end,
  after_use = function(self, player, use)
    if use.extra_data and use.extra_data.target then
      local target = use.extra_data.target
        -- 令其将手牌数摸至其体力上限
      if target:getHandcardNum() < target.maxHp then
        local n = target.maxHp - target:getHandcardNum()
        target:drawCards(n, "3s__heming")
      end
    end
  end,
})

return heming_jink
